<?php
/**
 * User: ikin
 * Date: 16/10/23
 * Time: 下午17:45
 */
namespace DesignArt\Post\Managers;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use DesignArt\Post\Exceptions\InvalidParameterException;
use DesignArt\Post\Exceptions\PostNotFoundException;
use DesignArt\Post\Repositories\PostRepository;

class PostManager
{
    /**
     * @var PostRepository
     */
    protected $postRepository;

    /**
     * PostManager constructor.
     * @param PostRepository $postRepository
     */
    public function __construct(PostRepository $postRepository)
    {
        $this->postRepository = $postRepository;
    }

    /**
     * 根据ID查询POST
     *
     * @param postID
     * @throws PostNotFoundException
     */
    public function find($postID, $userID = null)
    {
        $conditions = ['id' => $postID];

        if ( ! is_null($userID) ) {
            $conditions['uid'] = $userID;
        }

        $post = $this->postRepository->findBy($conditions);

        if ( ! $post ) {
            throw new PostNotFoundException;
        }

        return $post;
    }

    /**
     * 批量查post
     *
     * @param $postIDs
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function get($postIDs)
    {
        return $this->postRepository->get($postIDs);
    }

    /**
     * 写文章
     *
     * @param $attribute [field array]
     * @return bool|\Illuminate\Database\Eloquent\Model
     */
    public function create(array $attributes)
    {
        $attributes = $this->filterCreateAttributes($attributes);

        return $this->postRepository->create($attributes);
    }

    /**
     * 删除post
     *
     * @param $ID
     * @param array $attributes
     * @return mixed
     */
    public function delete($ID)
    {
        return $this->postRepository->update($ID, array('delete_process' => 1));
    }

    /**
     * 更新
     *
     * @param $ID
     * @param $attributes
     * @return bool
     */
    public function update($ID, $attributes)
    {
        return $this->postRepository->update($ID, $attributes);
    }

    /**
     * 分页查询post详情
     *
     * @param $conditions
     * @param $orderColumn
     * @param $orderDirection
     * @param $cursor
     * @param $size
     * @return Collection
     */
    public function getByPage($conditions, $orderColumn, $orderDirection, $cursor, $size, $fields)
    {
        return $this->postRepository->getByPage($conditions, $orderColumn, $orderDirection, $cursor, $size, $with = [], $fields);
    }

    /**
     * @param $partnerID
     * @param $pageSize
     * @param $orderColumn
     * @param $orderDirection
     * @param null $cursorPage
     * @return mixed
     */
    public function getPostsPaginateBy($supplierID, $pageSize, $orderColumn, $orderDirection, $cursorPage = null)
    {
        $conditions = [
            'author_id' => $supplierID,
            'delete_process' => 0
        ];

        $posts = $this->postRepository->getPaginate($conditions, $pageSize, $orderColumn, $orderDirection, $cursorPage);

        return $posts;
    }

    /**
     * 获取游记分页
     *
     * @param $pageSize
     * @param $orderColumn
     * @param $orderDirection
     * @param null $cursorPage
     * @param string $type | all, recommended
     * @return mixed
     */
    public function getPostsPaginate($pageSize, $orderColumn, $orderDirection, $type = 'all', $cursorPage = null)
    {
        $conditions = ['delete_process' => 0];

        $allTypes = ['all', 'recommended'];

        switch ($type) {
            case 'recommended':
                $conditions = function ($query) use ($conditions) {
                    $query->where($conditions);
                    $query->where('recommend_time', '>', '0');
                };
                break;

            default:
                break;
        }

        $posts = $this->postRepository->getPaginate($conditions, $pageSize, $orderColumn, $orderDirection, $cursorPage);

        return $posts;
    }

    /**
     * 字段自增
     *
     * @param $postID
     */
    public function incrementLike($postID)
    {
        $conditions = array('id' => $postID);
        $this->postRepository->increment($conditions, 'like_count');
    }

    /**
     * 字段自减
     *
     * @param $postID
     */
    public function decrementLike($postID)
    {
        $conditions = array('id' => $postID);
        $this->postRepository->decrement($conditions, 'like_count');
    }

    /**
     * 字段自增
     *
     * @param $postID
     */
    public function incrementComment($postID)
    {
        $conditions = array('id' => $postID);
        $this->postRepository->increment($conditions, 'comment_count');
    }

    /**
     * 字段自减
     *
     * @param $postID
     */
    public function decrementComment($postID)
    {
        $conditions = array('id' => $postID);
        $this->postRepository->decrement($conditions, 'comment_count');
    }

}